# coding:utf8
"""
author:shadu@foxmail.com
"""
from bs4 import BeautifulSoup

from core.http_content import get_html_safely
from core.proxyqueue import ProxyList
from core.network import is_ipaddr, is_port
from zeander.zeander import ZLOG

__version__ = '1.0'

SOURCE = 'free-proxy-list.net'
URL = 'https://free-proxy-list.net/'


def save(proxy=None):
    if proxy and isinstance(proxy, str):
        ProxyList.put(proxy)


def parse(html):
    proxy_count = 0
    bs = BeautifulSoup(html, 'html.parser')
    for bs_tr in bs.find('table', {'id': 'proxylisttable'}).find('tbody').findAll('tr'):
        bs_tds = bs_tr.findAll('td')
        proxy_ip = bs_tds[0].string
        proxy_port = bs_tds[1].string
        if is_ipaddr(proxy_ip) and is_port(proxy_port):
            proxy = '%s:%s' % (proxy_ip, proxy_port)
            save(str(proxy))
            proxy_count += 1
    ZLOG.info('Got %d proxies from %s' % (proxy_count, SOURCE))


def collect():
    html = get_html_safely(URL)
    # if __debug__:
    #    html = temp_str
    if html == '-':
        ZLOG.alert('Failed to get HTML from %s' % URL)
    else:
        parse(html)


if __name__ == '__main__':
    collect()
